/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bttk.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bttk.entity.TCustomer;
import com.thinkgem.jeesite.modules.bttk.service.TCustomerService;

/**
 * 客户资料管理Controller
 * @author soyatec
 * @version 2016-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/bttk/tCustomer")
public class TCustomerController extends BaseController {

	@Autowired
	private TCustomerService tCustomerService;
	
	@ModelAttribute
	public TCustomer get(@RequestParam(required=false) String id) {
		TCustomer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCustomerService.get(id);
		}
		if (entity == null){
			entity = new TCustomer();
		}
		return entity;
	}
	
	@RequiresPermissions("bttk:tCustomer:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCustomer tCustomer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCustomer> page = tCustomerService.findPage(new Page<TCustomer>(request, response), tCustomer); 
		model.addAttribute("page", page);
		return "modules/bttk/tCustomerList";
	}

	@RequiresPermissions("bttk:tCustomer:view")
	@RequestMapping(value = "form")
	public String form(TCustomer tCustomer, Model model) {
		model.addAttribute("tCustomer", tCustomer);
		return "modules/bttk/tCustomerForm";
	}

	@RequiresPermissions("bttk:tCustomer:edit")
	@RequestMapping(value = "save")
	public String save(TCustomer tCustomer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCustomer)){
			return form(tCustomer, model);
		}
		tCustomerService.save(tCustomer);
		addMessage(redirectAttributes, "保存客户资料管理成功");
		return "redirect:"+Global.getAdminPath()+"/bttk/tCustomer/?repage";
	}
	
	@RequiresPermissions("bttk:tCustomer:edit")
	@RequestMapping(value = "delete")
	public String delete(TCustomer tCustomer, RedirectAttributes redirectAttributes) {
		tCustomerService.delete(tCustomer);
		addMessage(redirectAttributes, "删除客户资料管理成功");
		return "redirect:"+Global.getAdminPath()+"/bttk/tCustomer/?repage";
	}

}